package top.viya.hello2.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SampleXxlJob {

    @XxlJob("demoJobHandler1")
    public void demoJobHandler() throws Exception {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, Hello World.");

        XxlJobHelper.log("启动参数为：" + param);

        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }

    @XxlJob("demoJobHandler2")
    public void demoJobHandler2() throws Exception {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, Hello World.");
        XxlJobHelper.log("启动参数为：" + param);
        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        XxlJobHelper.handleFail("任务失败了~哈哈");
    }

    @XxlJob(value = "demoJobHandler3", init = "init", destroy = "destroy")
    public void demoJobHandler3() throws Exception {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, Hello World.");
        XxlJobHelper.log("启动参数为：" + param);
        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        XxlJobHelper.handleSuccess("任务成功了~哈哈");
    }

    public void init() {
        XxlJobHelper.log("init方法被调用");
    }

    public void destroy() {
        XxlJobHelper.log("destroy方法被调用");
    }
}
